package cn.com.cifi.mars.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.cifi.mars.bean.bo.DictionaryCodeBO;
import cn.com.cifi.mars.bean.dto.DictionaryDto;
import cn.com.cifi.mars.bean.vo.sys.rsp.DictionaryVo;
import cn.com.cifi.mars.constant.IsDisableEnum;
import cn.com.cifi.mars.entity.common.SysDictionary;
import cn.com.cifi.mars.mapper.mars.SysDictionaryMapper;
import cn.com.cifi.mars.service.SysDictionaryService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yyd
 * @since 2019-09-12
 */
@Service
public class SysDictionaryServiceImpl extends ServiceImpl<SysDictionaryMapper, SysDictionary> implements SysDictionaryService {

	@Autowired
	private SysDictionaryMapper sysDictionaryMapper;
	
	//请选择
	private static final String PLEASE_CHOOSE="-1";
	
	/**
	 * 数据字典父级类型下拉框
	 */
	@Override
	public List<Map<String, Object>>  queryDictionarySelect() {
		QueryWrapper <SysDictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("dict_code", "dict_name","dict_value");
        List<Map<String, Object>> list = sysDictionaryMapper.selectMaps(queryWrapper);
        Map<String,Object>map2=new HashMap<String, Object>();
        map2.put("dict_code",PLEASE_CHOOSE);
        map2.put("dict_value",PLEASE_CHOOSE);        
        map2.put("dict_name", "请选择");
        list.add(0,map2);
        return list;
	}

	
	/**
	 * 分页查询字典
	 * @return
	 */
	@Override
	public DictionaryVo querySysDictionaryPage(String dictName,String typeCode,String current,String pageSize) {
		DictionaryVo vo=new DictionaryVo();
		Page<DictionaryDto>pageDto=new Page<>();
		//参数当前页
		pageDto.setCurrent(Integer.parseInt(current));
		//参数每页数量
		pageDto.setSize(Integer.parseInt(pageSize));

		List<DictionaryDto>list=sysDictionaryMapper.selectSysDictionaryPage(pageDto,dictName,typeCode);
		list.forEach(x->{
			String is_enable=x.getIsEnable();
			if(is_enable.equals(IsDisableEnum.NO.getKey()+"")) {
				is_enable=IsDisableEnum.NO.getValue()+"";
			}else {
				is_enable=IsDisableEnum.YES.getValue()+"";
			}
			x.setIsEnable(is_enable);
		});
		vo.setCurrent(pageDto.getCurrent());
		vo.setPages(pageDto.getPages());
		vo.setPageSize(pageDto.getSize());
		vo.setTotal(pageDto.getTotal());
		vo.setList(list);
		return vo;
	}

	/**
	 * 根据字典类型code显示不同的下拉框数据
	 */
	@Override
	public List<Map<String, Object>> queryDictionaryByTypeCode(String typeCode) {
		QueryWrapper<SysDictionary>qw=new QueryWrapper<>();
		qw.select("dict_code","dict_name","is_enable","dict_value").eq("type_code", typeCode);
        List<Map<String, Object>> list =sysDictionaryMapper.selectMaps(qw);
        Map<String,Object>map2=new HashMap<String, Object>();
        map2.put("dict_code", "");
        map2.put("dict_name", "请选择");
        map2.put("is_enable", "0");
        map2.put("dict_value", "");
        list.add(0,map2);
        return list;
	}

	/**
	 * 根据产品构成code获取字典信息
	 */
	@Override
	public List<DictionaryCodeBO> selectByCodes(List<String> productCodes) {
		return sysDictionaryMapper.selectByCodes(productCodes);
	}

	/**
	 * 根据code获取字典信息
	 */
	@Override
	public List<SysDictionary> selectDictionaryByType(List<String> codes) {
		return sysDictionaryMapper.selectDictionaryByType(codes);
	}

	@Override
	public SysDictionary selectDictionaryByCode(String code) {
		return sysDictionaryMapper.selectDictionaryByCode(code);
	}

	@Override
	public List<SysDictionary> selectListByTypeCode(String code) {
		QueryWrapper<SysDictionary> qw = new QueryWrapper<>();
		qw.eq("type_code", code).eq("is_enable", IsDisableEnum.NO.getKey());
        List<SysDictionary> list =sysDictionaryMapper.selectList(qw);
		return list;
	}

	@Override
	public String selectDictValueByTypeCode(String typeCode){
		return sysDictionaryMapper.selectDictValueByTypeCode(typeCode);
	}

	@Override
	public SysDictionary selectDictionaryByTypeCode(String typeCode,String month) {
		QueryWrapper<SysDictionary>qw=new QueryWrapper<>();
		qw.eq("type_code", typeCode).eq("is_enable", IsDisableEnum.NO.getKey()).like("dict_value",month);
        List<SysDictionary> list =sysDictionaryMapper.selectList(qw);
		if(CollectionUtils.isNotEmpty(list)) {
			return list.get(0);
		}
		return null;
	}
	
}
